package cn.bs.busuo.front.account.controller;

import cn.bs.busuo.front.account.service.IFriendService;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Slf4j
@RequestMapping("/front/users/friend")
@Api(tags = "3.用户好友接口")
public class FriendController {
    @Autowired
    private IFriendService friendService;

    /**
     * 用户添加好友
     */
    @PostMapping("/{id:[0-9]+}/add")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户添加好友")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "好友ID", required = true, dataType = "long")
    })
    public JsonResult addNewFriend(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                   @ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal) {
        friendService.addNewFriend(principal.getId(),id);
        return JsonResult.ok();
    }

    /**
     * 用户删除好友
     */
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/account/user/normal')")
    @ApiOperation("用户删除好友")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "好友ID", required = true, dataType = "long")
    })
    public JsonResult deleteFriend(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                   @ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal) {
        friendService.deleteFriend(principal.getId(),id);
        return JsonResult.ok();
    }
}